package com.example.demo.Controller;

import cn.edu.zut.dao.jdbcTemplateDemo.UserDO;
import cn.edu.zut.dao.mybatis.UserMapperXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class UsercontrollerForward {
    @Autowired
    UserMapperXml userMapperXml;
    @ResponseBody
    @RequestMapping(value = "/logincheck",method = RequestMethod.GET)
    public String login(Model model, HttpServletRequest request){
        //request就是浏览器发过来的所有的请求消息
        //1、能够拿到浏览器中用户名密码
        //首先分析，是不是前段传值过来有问题
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(username);
        System.out.println(password);

        //2、那这个用户名和密码去数据库里面比一下,这里去访问数据库层了
        UserDO user = userMapperXml.loginCheck(username,password);

        System.out.println(user);
        if(user!=null){

            //跳转之前记录一下已经登陆成功了，把登陆信息放到session
            HttpSession session=request.getSession();
            session.setAttribute("user",user);
            //跳转index.html
            model.addAttribute("username",user.getUserid());
            return "index";
        }else{
            model.addAttribute("errorinfo","用户名或者密码错误");
            return "login";
        }
    }
    @RequestMapping("/Login")
    public  String toLogin(){
        return "login";
    }
}